package com.newgrand.annexserver.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean;
import com.newgrand.annexserver.datasource.NgDataSourceContextHolder;
import com.newgrand.annexserver.datasource.NgDynamicRoutingDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Configuration
public class NgDataSourceConfig {

    @Value("${ngsoft.dataSource.driverClassName}")
    private String driverClassName;

    @Value("${ngsoft.dataSource.url}")
    private String url;

    @Value("${ngsoft.dataSource.username}")
    private String username;

    @Value("${ngsoft.dataSource.password}")
    private String password;


    @Bean("ngDynamicDataSource")
    public DataSource ngDynamicDataSource() {
        NgDynamicRoutingDataSource dynamicRoutingDataSource = new NgDynamicRoutingDataSource();
        Map<Object, Object> dataSourceMap = new HashMap<>();

        DataSource dataSource = null;
        Properties p = new Properties();
        p.put("url", url);
        p.put("driverClassName", driverClassName);
        p.put("username", username);
        p.put("password", password);
        try {
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (Exception e) {
            e.printStackTrace();
        }
        dataSourceMap.put(NgDataSourceContextHolder.defaultDataSourceKey, dataSource);
        NgDataSourceContextHolder.setDataSourceKey(NgDataSourceContextHolder.defaultDataSourceKey);

        dynamicRoutingDataSource.setTargetDataSources(dataSourceMap);
        return dynamicRoutingDataSource;
    }

//      @Bean("ngSqlSessionFactoryBean")
//      public SqlSessionFactoryBean ngSqlSessionFactoryBean() {
//            SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
//            sqlSessionFactoryBean.setDataSource(ngDynamicDataSource());
//
//            PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
//            try {
//                  sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
//            } catch (IOException e) {
//                  e.printStackTrace();
//            }
//            return sqlSessionFactoryBean;
//      }

    @Bean("ngSqlSessionFactoryBean")
    public MybatisSqlSessionFactoryBean ngSqlSessionFactoryBean() {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(ngDynamicDataSource());

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            sqlSessionFactoryBean.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sqlSessionFactoryBean;
    }


    @Bean("ngTransactionManager")
    public PlatformTransactionManager ngTransactionManager() {
        return new DataSourceTransactionManager(ngDynamicDataSource());
    }

}
